package com.neu.questionnaire_system.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.questionnaire_system.entity.Cluster;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xingle
 * @since 2022-10-31
 */
@Mapper
public interface ClusterMapper extends BaseMapper<Cluster> {
    @Insert("insert into cluster values (#{cid},#{kind,jdbcType=VARCHAR},#{uid},#{isDeleted})")
    int insertCluster(@Param("cid")String cid, @Param("kind")String kind, @Param("uid")String uidm,@Param("isDeleted")String isDeleted);

    @Select("select * from cluster")
    List<Cluster> showCluster();
    //根据提供的用户uid查询其管理的群组
    /*@Select("select uid,cid,kind from cluster where uid =#{uid} and isDeleted=0")
    List<Cluster> showClusterByUid(@Param("uid") String uid);*/

    @Select("select uid,cid,kind from cluster where uid =#{uid} and isDeleted='0'")
    IPage<Map<String,Object>> showClusterByUid(@Param("page") Page page,@Param("uid")String uid);


    @Delete("delete from cluster where cid=#{cid}")
    int deleteCluster(@Param("cid")String cid);

    @Select("select * from cluster where kind like #{kind} and uid =#{uid} and isDeleted='0'")
    List<Cluster> findClusterByKind(@Param("kind")String kind, @Param("uid")String uid);

    //提供cid当前用户修改群组信息
    @Update("update cluster set kind=#{kind,jdbcType=VARCHAR} where cid=#{cid,jdbcType=VARCHAR}")
    int updateCluster(Map<String,Object> map);



    //使用xml来插入答者到群组
    int addAnswer_ClusterInfo(Map<String,Object> map);

    //查询答者名是否存在
    int queryExistAnswer_Cluster(Map<String,Object> map);

    //查询群组答者列表
    List<Map<String,Object>> showAnswer_Cluster(Map<String,Object> map);

    //根据答者名删除
    @Delete("delete from answer_cluster where aid=#{aid,jdbcType=VARCHAR}and cid=#{cid,jdbcType=VARCHAR}")
    int deleteAnswer_Cluster(Map<String,Object> map);
    /*@Delete("delete from answer_cluster where aid=#{aid,jdbcType=VARCHAR} and cid=#{cid,jdbcType=VARCHAR}")
    int deleteAnswer_Cluster(String aid,String cid);*/

    //用户根据答者名查询答者信息，返回一个包含答者信息的map
    List<Map<String,Object>> findAnswer_ClusterByAnswerName(Map<String,Object> map);

    //根据答者名判断答者是否存在，如果存在返回大于0
    int queryAnswer_ClusterIsEmpty(String AnswerName);
    //通过答者名查询其aid
    String queryAidByAnswerName(String answerName);
    @Insert("insert into answer_cluster(aid,cid,answerStatus) values(#{aid},#{cid},#{answerStatus})")
    int addAnswerCluster(@Param("aid")String aid,@Param("cid")String cid,@Param("answerStatus")String answerStatus);

    @Select("select count(1) from answer_cluster where cid =#{cid}")
    int ifAnswerInCluster(@Param("cid")String cid);

    

    @Update("update cluster set isDeleted=#{isDeleted,jdbcType=VARCHAR} where cid=#{cid,jdbcType=VARCHAR}")
    int updateClusterStatue(@Param("isDeleted")String isDeleted,@Param("cid")String cid);

    @Select("select count(1) from cluster where kind=#{kind}")
    int findSameName(@Param("kind")String kind);

    @Select("select kind from cluster where uid=#{uid} and isDeleted='0'")
    List<String> selectClusterName(@Param("uid")String uid);

    @Select("select * from cluster where kind=#{kind} and uid=#{uid} and isDeleted='0'")
    Cluster selectCidByKind(@Param("kind")String kind,@Param("uid")String uid);

    @Update("update answer_cluster set qid=#{qid} where cid=#{cid}")
    int updateByKind(@Param("cid")String cid,@Param("qid")String qid);


}
